package com.herenit.order_model.service.impl;

import com.herenit.order_model.dao.OmRecordDao;
import com.herenit.order_model.entity.OmRecord;
import com.herenit.order_model.enums.ExceptionEnums;
import com.herenit.order_model.exception.MyException;
import com.herenit.order_model.service.OmRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class OmRecordServiceImpl implements OmRecordService {

    @Autowired
    private OmRecordDao omRecordDao;

    @Override
    public Page<OmRecord> queryAllBackedRecords(int page, int size) {
        List<Integer> countCollection = Collections.singletonList(1);
        PageRequest request = PageRequest.of(page, size, Sort.Direction.ASC, "id");
        return omRecordDao.findByCountNotIn(countCollection, request);
    }

    @Override
    public OmRecord findById(Long id) {
        OmRecord omRecord;
        try {
            omRecord = omRecordDao.findById(id).get();
        } catch (Exception e) {
            throw new MyException(ExceptionEnums.DATA_NOT_EXIST);
        }
        return omRecord;
    }

    @Override
    public OmRecord save(OmRecord omRecord) {
        return omRecordDao.save(omRecord);
    }

}
